<?php

include('common.php');

function find_closing_call($domain, $line)
{
	$user = user();
	$result = sql_exec("SELECT * FROM Instances WHERE Domain='$domain' AND Line_Number > $line AND User='$user' ORDER BY Line_Number");

	$level = 1;
	while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
	{
		$lower_call = $row['Fxn_Call'];
		if (ctl_struct($lower_call))
		{
			$level++;
		}

		if (end_ctl_struct($lower_call))
		{
			$level--;
		}

		if ($level == 0)
		{
			return $row['Line_Number'];
		}
	}
}

function find_endul($domain, $line)
{
	$user = user();
	$result = sql_exec("SELECT * FROM Instances WHERE Domain='$domain' AND Line_Number > $line AND User='$user' ORDER BY Line_Number");

	$level = 1;
	while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
	{
		$lower_call = $row['Fxn_Call'];
		if ($lower_call == 'ul')
		{
			$level++;
		}

		if ($lower_call == 'endul')
		{
			$level--;
		}

		if ($level == 0)
		{
			return $row['Line_Number'];
		}
	}
}

function find_endol($domain, $line)
{
	$user = user();
	$result = sql_exec("SELECT * FROM Instances WHERE Domain='$domain' AND Line_Number > $line AND User='$user' ORDER BY Line_Number");

	$level = 1;
	while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
	{
		$lower_call = $row['Fxn_Call'];
		if ($lower_call == 'ol')
		{
			$level++;
		}

		if ($lower_call == 'endol')
		{
			$level--;
		}

		if ($level == 0)
		{
			return $row['Line_Number'];
		}
	}
}

function find_endselect($domain, $line)
{
	$user = user();
	$result = sql_exec("SELECT * FROM Instances WHERE Domain='$domain' AND Line_Number > $line AND User='$user' ORDER BY Line_Number");

	$level = 1;
	while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
	{
		$lower_call = $row['Fxn_Call'];
		if ($lower_call == 'select')
		{
			$level++;
		}

		if ($lower_call == 'endselect')
		{
			$level--;
		}

		if ($level == 0)
		{
			return $row['Line_Number'];
		}
	}
}


function delete_instance($domain, $line)
{
	$user = user();
	// delete all associated literals
	$sql = "DELETE FROM Vals WHERE Domain='$domain' AND Line_Number='$line' AND User='$user'";
	mysql_query($sql) or die("could not $sql: " . mysql_error());
	sql_exec("DELETE FROM Instances WHERE Domain='$domain' AND Line_Number=$line AND User='$user'");
	sql_exec("UPDATE Instances SET Line_Number=Line_Number-1 WHERE Domain='$domain' AND Line_Number > $line AND User='$user'");
	sql_exec("UPDATE Vals SET Line_Number=Line_Number-1 WHERE Domain='$domain' AND Line_Number > $line AND User='$user'");
}

// function that deletes the form opening, middle, and end, and the definition and Instances associated with it
function delete_form($domain, $line)
{
	global $user;
	// delete definition and the script associated with it
	$instance = sql_row1("SELECT * FROM Instances WHERE User='$user' AND Domain='$domain' AND Line_Number='$line'");
	$def_id = $instance['Var1'];
	$def = sql_row1("SELECT * FROM Definitions WHERE ID='$def_id' ");
	$def_name = $def['Fxn_Call'];
	sql_exec("DELETE FROM Definitions WHERE ID='$def_id'");
	sql_exec("DELETE FROM Instances WHERE Fxn_Call='$def_name' AND User='$user' ");

	// find endform
	$row = sql_row1("SELECT * FROM Instances WHERE Fxn_Call='endform' AND Line_Number>'$line' ORDER BY Line_Number");
	$endform_line = $row['Line_Number'];
	// now delete everything
	sql_exec("DELETE FROM Instances WHERE User='$user' AND Domain='$domain' AND Line_Number>='$line' AND Line_Number<='$endform_line' ");
}

$user = user();

$row = sql_row1("SELECT * FROM Instances WHERE Domain='$domain' AND Line_Number='$line' AND User='$user'");
$call = $row['Fxn_Call'];

// don't allow deleting endform
// delete close of forms at same time forms are deleted
// don't allow deleting endforms either
if ($call == 'form')
{
	delete_form($domain, $line);
} elseif ($call == 'ol') {
	$endol = find_endol($domain, $line);
	for ($l = $endol; $l >= $line; $l--)
	{
		delete_instance($domain, $l);
	}
} elseif ($call == 'ul') {
	$endul = find_endul($domain, $line);
	for ($l = $endul; $l >= $line; $l--)
	{
		delete_instance($domain, $l);
	}
} elseif ($call == 'select') {
	// delete everything in the middle for select too
	$end = find_endselect($domain, $line);
	for ($l = $end; $l >= $line; $l--)
	{
		delete_instance($domain, $l);
	}
} else {
	if (ctl_struct($call))
	{
		$closing_call = find_closing_call($domain, $line);
		delete_instance($domain, $closing_call);
	}

	delete_instance($domain, $line);
}

// maybe shouldn't update this line after all since it didn't actually change.
// this would have been useful if the file.svg#line had worked

update($user, $domain, $line-1);

header("Location: edit_page.php?domain=$domain");

?>



